package com.bc.ui;

import cn.waps.AppConnect;

import com.bc.db.DBHelper;
import com.bc.utils.DBUtils;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.MemoryCacheAware;
import com.nostra13.universalimageloader.cache.memory.impl.LRULimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.umeng.analytics.MobclickAgent;

import android.app.Application;
import android.content.Intent;
import android.graphics.Bitmap;

public class BtcApp extends Application {
	
	@Override
	public void onCreate() {
		super.onCreate();
		
		DBHelper.init(this);
		
		initDBNews();
		
		initImageLoader();
		
		
		MobclickAgent.setDebugMode( true );
		
	}
	
	public void initDBNews() {
		DBUtils.copyDbNewsIfNeeded(this);
	}
	
	private void initImageLoader() {

		ImageLoader imageLoader = ImageLoader.getInstance();
		DisplayImageOptions defaultDisplayImageOptions = new DisplayImageOptions.Builder()			
				.cacheInMemory(true)
				.cacheOnDisc(true)				
				.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
				.bitmapConfig(Bitmap.Config.ARGB_8888)				
				.build();
		int memoryCacheSize = (int) (Math.min(Runtime.getRuntime().maxMemory() / 8,8*1024*1024));
		MemoryCacheAware<String, Bitmap> memoryCache = new LRULimitedMemoryCache(memoryCacheSize);
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())						
				.memoryCache(memoryCache)			
				.defaultDisplayImageOptions(defaultDisplayImageOptions)
				.denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator())	
				.tasksProcessingOrder(QueueProcessingType.FIFO)
				.imageDownloader(new BaseImageDownloader(this))
				.build();
		imageLoader.init(config);
	}
}
